package com.linewow.xhyy.nihaonew.adapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.activity.BeautyPhotoActivity;
import com.linewow.xhyy.nihaonew.activity.TopicDetailActivity;
import com.linewow.xhyy.nihaonew.activity.TranslateActivity;
import com.linewow.xhyy.nihaonew.entity.image.ImagesInfo;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.linewow.xhyy.nihaonew.util.CommonUtil;
import com.linewow.xhyy.nihaonew.util.Help;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
/**
 * Created by LXR on 2016/9/7.
 */
public class ImagesVerticalAdapter extends RecyclerView.Adapter {
    private LayoutInflater layoutInflater;
    private List<ImagesInfo> imagesInfos;
    private Context context;
    public ImagesVerticalAdapter(Context context, List<ImagesInfo> imagesInfos) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(this.context);
        this.imagesInfos = imagesInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_topic_detail_images, null);
        return new ImagesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ImagesInfo imagesInfo = imagesInfos.get(position);
//        Picasso.with(context).load(imagesInfo.pathbase + HttpModel.IMAGEBIGSUFFIX).fit().centerCrop().error(R.mipmap.delete2).into(((ImagesViewHolder) holder).item3TopicDetailImg);

        Picasso.with(context).load((imagesInfo.pathbase + HttpModel.IMAGEBIGSUFFIX).replace("m.en1on1.com", "huibenabc.com")).error(R.mipmap.err_topic).transform(CommonUtil.getTransformation(context))
                .into(((ImagesViewHolder) holder).item3TopicDetailImg);

        if(imagesInfo.ext.equals("null")){
            ((ImagesViewHolder) holder).transRela.setVisibility(View.GONE);
        }else{
            ((ImagesViewHolder) holder).transRela.setVisibility(View.VISIBLE);
            ((ImagesViewHolder) holder).transTv.setText(imagesInfo.ext);
        }
    }

    @Override
    public int getItemCount() {
        return imagesInfos.size();
    }

    class ImagesViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item3_topic_detail_img)
        ImageView item3TopicDetailImg;
        @Bind(R.id.item3_topic_detail_rela)
        RelativeLayout transRela;
        @Bind(R.id.item3_topic_detail_trans_img)
        ImageView transImg;
        @Bind(R.id.item3_topic_detail_trans_tv)
        TextView transTv;

        ImagesViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            item3TopicDetailImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ImagesInfo info = imagesInfos.get(ImagesViewHolder.this.getAdapterPosition());
                    Intent intent = new Intent(context, BeautyPhotoActivity.class);
                    intent.putExtra("uri", info.pathbase + HttpModel.IMAGEBIGSUFFIX);
                    Log.e("ImagesVerticalAdapter", "点击了-->" + "地址" + info.pathbase + HttpModel.IMAGEBIGSUFFIX);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                        final android.support.v4.util.Pair<View, String>[] pairs = Help.createSafeTransitionParticipants
                                ((Activity) context, false, new android.support.v4.util.Pair<>(item3TopicDetailImg, "beauty"));
                        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context, pairs);
                        context.startActivity(intent, options.toBundle());
                    } else {
                        context.startActivity(intent);
                    }

                }
            });

            item3TopicDetailImg.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    if(((TopicDetailActivity)(context)).getEditFlag()){
                        return true;
                    }
                    return false;
                }
            });

            transImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ImagesInfo info = imagesInfos.get(ImagesViewHolder.this.getAdapterPosition());
                    Intent intent=new Intent(context, TranslateActivity.class);
                    intent.putExtra("info",info.ext);
                    context.startActivity(intent);
                }
            });

        }
    }
}
